package com.javasm.mb_store_api_final.sys_control.rolecontrol.sys_role.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.javasm.mb_store_api_final.sys_control.rolecontrol.sys_role.entity.SysRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * (SysRole)表数据库访问层
 *
 * @author makejava
 * @since 2024-07-29 20:18:46
 */
public interface SysRoleDao extends BaseMapper<SysRole> {

    @Select("SELECT DISTINCT pos.pos_id, role.role_id, role.role_name, role.role_state, pos.pos_code, pos.pos_name, depart.depart_name " +
            "FROM sys_user user " +
            "JOIN sys_pos pos ON user.pos_id = pos.pos_id " +
            "JOIN sys_role role ON user.role_id = role.role_id " +
            "JOIN sys_depart depart ON user.depart_id = depart.depart_id " +
            "ORDER BY pos.pos_id")
    List<SysRole> selectAllRole();


    @Insert({
            "INSERT INTO sys_role (role_name, role_state) VALUES (#{roleName}, #{roleState});",
            "INSERT INTO sys_pos (pos_name) VALUES (#{posName});",
            "INSERT INTO sys_depart (depart_name) VALUES (#{departName})"
    })
    void addRole(@Param("role") SysRole role);


    @Update("<script>"
            + "UPDATE sys_role SET "
            + "<if test='pos.pos_code != null'>pos_code = #{pos.posCode}, </if>"
            + "<if test='pos.pos_name != null'>pos_name = #{pos.posName}, </if>"
            + "<if test='role.role_name != null'>role_name = #{role.roleName}, </if>"
            + "<if test='depart.depart_name != null'>depart_name = #{depart.departName}, </if>"
            + "<if test='role.roleState != null'>role_state = #{role.roleState}, </if>"
            + "WHERE role_id = #{rid}"
            + "</script>")
    void updateRole(@Param("rid") Integer roleId,@Param("role") SysRole role);

    @Delete("DELETE FROM sys_role WHERE role_id = #{roleId}")
    void deleteByRId(Integer roleId);

    @Select({
            "<script>",
            "SELECT DISTINCT pos.pos_id, role.role_id, role.role_name, role.role_state, pos.pos_code, pos.pos_name, depart.depart_name ",
            "FROM sys_user user ",
            "JOIN sys_pos pos ON user.pos_id = pos.pos_id ",
            "JOIN sys_role role ON user.role_id = role.role_id ",
            "JOIN sys_depart depart ON user.depart_id = depart.depart_id ",
            "WHERE 1=1 ", // 始终为真，确保后续条件可以正确添加
            "<if test=\"departName != null and departName.trim().length() > 0\">",
            "AND depart.depart_name = #{departName} ",
            "</if>",
            "<if test=\"roleState != null and roleState.trim().length() > 0\">",
            "AND role.role_state = #{roleState} ",
            "</if>",
            "<if test=\"posName != null and posName.trim().length() > 0\">",
            "AND pos.pos_name LIKE '%' || #{posName} || '%' ",
            "</if>",
            "<if test=\"roleName != null and roleName.trim().length() > 0\">",
            "AND role.role_name LIKE '%' || #{roleName} || '%' ",
            "</if>",
            "ORDER BY pos.pos_id",
            "</script>"
    })
    List<SysRole> queryBy(SysRole role);
    @Select("SELECT COUNT(*) FROM (" +
            "SELECT pos.pos_code, pos.pos_name, pos.pos_state, role.role_name, depart.depart_name" +
            " FROM sys_user user " +
            " JOIN sys_pos pos ON user.pos_id = pos.pos_id" +
            " JOIN sys_role role ON user.role_id = role.role_id " +
            " JOIN sys_depart depart ON user.depart_id  = depart.depart_id" +
            ") AS subquery")
    Long selectAllCount();
}

